// -----------------------------------------------------------------------
//  <copyright file="EventSourcedEntityBase.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2023 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2023-12-16 22:24</last-date>
// -----------------------------------------------------------------------

namespace DaprPlus.Domain.Entity;

/// <summary>
/// 包含事件溯源功能的实体基类
/// </summary>
public abstract class EventSourcedEntityBase : HasDomainEventEntityBase, IEventSourced
{
    /// <summary>
    /// 应用事件。
    /// </summary>
    /// <param name="domainEvent">要应用的事件。</param>
    public void ApplyEvent(DomainEvent domainEvent)
    {
        _DomainEvents.Add(domainEvent);
    }

    /// <summary>
    /// 从历史记录加载。
    /// </summary>
    /// <param name="history">要加载的历史记录。</param>
    public void LoadFromHistory(IEnumerable<DomainEvent> history)
    {
        foreach (var domainEvent in history)
        {
            ApplyEvent(domainEvent);
        }
    }
}
